package com.inspinia.base.util;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * Created by veblen on 15-5-21.
 */
public class CollectionUtil {

    /**
     * 判断集合不为空
     *
     * @param c
     * @return true表示集合不为空
     */
    public static boolean isNotEmpty(Collection c) {
        return null != c && c.size() > 0;
    }

    /**
     * 判断集合为空
     *
     * @param c 集合
     * @return true表示集合为空
     */
    public static boolean isEmpty(Collection c) {
        return !isNotEmpty(c);
    }


    /**
     * 去除重复
     *
     * @param list
     */
    public static void removeDuplicate(List list) {
        HashSet set = new HashSet(list);
        list.clear();
        list.addAll(set);
    }


    /**
     * 获取多个实体的id字符串，id之间用“,”隔开。
     *
     * @param coll      实体列表
     * @param filedName 字段名称
     * @return
     * @author 何顺
     */
    public static String list2string(Collection<?> coll, String filedName) {
        Iterator it = coll.iterator();
        String ids = "";

        String filed = "";
        if (StringUtils.isNotEmpty(filedName)) {
            filed = filedName;
        } else {
            filed = "id";
        }
        while (it.hasNext()) {
            Object entity = it.next();
            String id = null;
            try {
                id = (Long) PropertyUtils.getProperty(entity, filed) + "";
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            ids += "," + id;
        }
        if (!StringUtils.isBlank(ids)) {
            ids = ids.substring(1);
        }
        return ids;
    }

    /**
     * 获取多个实体的id字符串，id之间用“,”隔开。
     *
     * @param coll 实体列表
     * @return
     * @author 何顺
     */
    public static String list2string(List<?> coll) {
        if (isNotEmpty(coll)) {
            Iterator it = coll.iterator();
            String ids = "";
            while (it.hasNext()) {
                String id = String.valueOf(it.next());
                ids += "," + id;
            }
            if (!StringUtils.isBlank(ids)) {
                ids = ids.substring(1);
            }
            return ids;
        }
        return null;
    }

    /**
     * string数组的id转成Long型的集合
     *
     * @param ids
     * @return
     */
    public static List<Long> stringIds2LongIds(String[] ids) {
        List<Long> idList = new ArrayList<>();
        if (ids != null && ids.length > 0) {
            for (String id : ids) {
                if (StringUtils.isNotEmpty(id)) {
                    idList.add(Long.valueOf(id));
                }
            }
        }
        return idList;
    }


    /**
     * 获取多个实体的id字符串，id之间用“,”隔开。
     *
     * @param ids id数组
     * @return id字符串，id之间用“,”隔开。
     * @author 何顺
     */
    public static String list2string(String[] ids) {

        String idStr = "";
        for (String id : ids) {
            if (!StringUtils.isBlank(id)) {
                //解决有时id带单引号的问题。
                if (id.startsWith("'")) {
                    idStr += "," + id;
                } else {
                    idStr += ",'" + id + "'";
                }
            }
        }
        if (!StringUtils.isBlank(idStr)) {
            idStr = idStr.substring(1);
        }
        return idStr;
    }

    /**
     * 获取多个实体的id字符串，id之间用“,”隔开。
     *
     * @param coll 实体列表
     * @return
     * @author 何顺
     */
    public static String list2string(Collection<?> coll) {

        Iterator it = coll.iterator();
        String ids = "";
        while (it.hasNext()) {
            Object entity = it.next();
            String id = null;
            try {
                id = (Long) PropertyUtils.getProperty(entity, "id") + "";
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            ids += "," + id;
        }
        if (!StringUtils.isBlank(ids)) {
            ids = ids.substring(1);
        }
        return ids;
    }

}
